<?php

namespace app\controller;

use app\traits\ControllerTrait;

class AdminGoods extends ControllerTrait
{
    public function Index()
    {
    }

    /**
     * Author: yuShuangJiang
     * Time: 2021/6/27 9:58
     * 所有商品信息
     */
    public function GoodsList()
    {
        $goodsList = app("adminGoodsModel")
//            ->field(["id","image","icon","menu_path","html"])
            ->select()
            ->toArray();

        $data = array(
            'msg' => '商品列表',
            'goodsList' => $goodsList
        );
        $this->success($data);
    }

    /**
     * Author: yuShuangJiang
     * Time: 2021/6/27 9:58
     * 增加/修改 商品信息
     * 传入商品ID则为修改商品属性。否则为新增
     */
    public function AddGoods()
    {
        $param = $this->request->param();
        $rule = [
            "image|商品图片" => "require",
            "recommend_image|推荐图" => "require",
            "slider_image|轮播图" => "require",
            "store_name|商品名称" => "require",
            "store_info|商品简介" => "require",
            "cate_id|分类id" => "require",
            "price|商品价格" => "require",
            "vip_price|会员价格" => "require",
            "ot_price|市场价" => "require",
            "sales|销量" => "require",
            "stock|库存" => "require",
            "unit_name|单位名" => "require",
            "is_show|状态（0：未上架，1：上架）" => "require",
            "cost|成本价" => "require",
            "browse|浏览量" => "require",
        ];
        $this->validate($param, $rule);

        if (empty($param['id'])) {
            $addUser = app("adminGoodsModel");
            $param['create_time'] = time();
        } else {
            $addUser = app("adminGoodsModel")->where(['id' => $param['id'],])->find();
            $param['update_time'] = time();
        }

        $addUser->save($param);
        $this->success(['msg' => '操作成功']);
    }
}
